export default {
  namespaced: true,

  state: () => ({
    userName: '', // 初始化为空
    userId: null,  // 初始化为null
    userType: undefined,
    permission: false,
    phone: ''     // 可选：如果需要存储手机号
  }),

  mutations: {
    SET_USER_INFO: (state: any, payload) => {
      state.userName = payload.username || payload.userName;
      state.userId = payload.userId;
      state.userType = payload.role; // 使用后端返回的role
      state.phone = payload.phone;   // 可选
    },
    SET_USERTYPE: (state: any, userType: number) => {
      state.userType = userType;
    },
    SET_PERMISSION: (state: any, permission) => {
      state.permission = permission;
    },
    CLEAR_USER: (state: any) => {
      state.userName = '';
      state.userId = null;
      state.userType = undefined;
      state.phone = '';
    }
  },

  actions: {
    loginSuccess({ commit }, userData) {
      commit('SET_USER_INFO', {
        username: userData.username,
        userId: userData.userId,
        role: userData.role,
        phone: userData.phone
      });
    },
    logout({ commit }) {
      commit('CLEAR_USER');
    }
  }
};